package com.cwits.wifi.ui.dialog;

import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cwits.wifi.R;
import com.cwits.wifi.base.BaseDialogFragment;
import com.cwits.wifi.data.beans.BitmapInfo;
import com.cwits.wifi.util.Dbug;
import com.cwits.wifi.util.TimeFormater;

import java.util.List;

/**
 * Created by Administrator on 2016-05-03.
 */
public class SelectUpdataDialog extends BaseDialogFragment implements View.OnClickListener {
    public interface mOnSelVdeListener {
        void OnSel(long start_time, long stop_time);
        void onCancle();
    }

    private boolean isUpdate = false;
    private List<BitmapInfo> bitmapInfoList;
    public int a = -1;
    public int b = -1;
    private mOnSelVdeListener mOnSelectListener;

    public void setOnEndListener(mOnSelVdeListener ls) {
        mOnSelectListener = ls;
    }

    public static SelectUpdataDialog newInstance() {
        return new SelectUpdataDialog();
    }

    public void setBitmapList(List<BitmapInfo> list) {
        isUpdate = true;
        bitmapInfoList = list;
    }

    public void updateBitmap() {
        if (bitmapInfoList.size() >= 18) {
            a = 9;
            b = 17;
        } else if (bitmapInfoList.size() >= 10) {
            b = bitmapInfoList.size() - 1;
            a = b - 8;
        } else if (bitmapInfoList.size() < 10) {
            b = bitmapInfoList.size() - 1;
            a = 0;
        }

        adapter.notifyDataSetChanged();
        mLlLoad.setVisibility(View.GONE);
        mGvVideo.setVisibility(View.VISIBLE);
    }

    private TextView mTvCancle, mTvSave;
    private GridView mGvVideo;
    private VideoAdapter adapter;
    private LinearLayout mLlLoad;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.select_undata_dialog, container, false);

        mTvCancle = (TextView) v.findViewById(R.id.select_updata_tv_cancle);
        mTvSave = (TextView) v.findViewById(R.id.select_updata_tv_save);
        mLlLoad = (LinearLayout) v.findViewById(R.id.select_ll_load_sos);
        mTvSave.setOnClickListener(this);
        mTvCancle.setOnClickListener(this);
        mGvVideo = (GridView) v.findViewById(R.id.select_updata_gv);
        adapter = new VideoAdapter();
        mGvVideo.setAdapter(adapter);
        mGvVideo.setSelection(10);
        mGvVideo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //TextView tv = (TextView) view.findViewById(R.id.item_select_iv_select);
                if (a == -1) {
                    if (b != i) {
                        a = i;
                    } else {
                        b = -1;
                    }

                } else if (a != -1 && b == -1) {
                    if (a != i) {
                        b = i;
                    } else {
                        a = -1;
                    }
                } else if (a != -1 && b != -1) {
                    if (a == i) {
                        a = -1;
                    } else if (b == i) {
                        b = -1;
                    } else {
                        a = b;
                        b = i;
                    }
                }
                adapter.notifyDataSetChanged();
            }
        });

        return v;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.select_updata_tv_save:
                if (bitmapInfoList == null)
                    return;
                if (isUpdate == false)
                    return;
                if (a != -1 && b != -1&&Math.abs(a - b) < 25) {
                    if (mOnSelectListener != null) {
                        if (a > b) {
                            long start_time = bitmapInfoList.get(b).getTime();
                            long stop_time = bitmapInfoList.get(a).getTime();
                            Dbug.i("SelectUpdataDialog", "start_time:" + TimeFormater.formatYMDHMS(start_time) + "----stop_time:" + TimeFormater.formatYMDHMS(stop_time));
                            mOnSelectListener.OnSel(start_time, stop_time);
                        } else {
                            long start_time = bitmapInfoList.get(a).getTime();
                            long stop_time = bitmapInfoList.get(b).getTime();
                            mOnSelectListener.OnSel(start_time, stop_time);
                            Dbug.i("SelectUpdataDialog", "start_time:" + TimeFormater.formatYMDHMS(start_time) + "----stop_time:" + TimeFormater.formatYMDHMS(stop_time));
                        }
                    }

                    dismiss();
                } else if(a != -1 && b != -1&&Math.abs(a - b) >= 25){
                    showToastLong(getString(R.string.dialog_sel_video_toolong));
                }else {
                    showToastLong(getString(R.string.dialog_sel_video));
                }

                break;
            case R.id.select_updata_tv_cancle:
                if (mOnSelectListener != null)
                    mOnSelectListener.onCancle();
                dismiss();
                break;
        }
    }

    private class VideoAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            if (null == bitmapInfoList) {
                return 0;
            }
            if (bitmapInfoList.size() > 61) {
                return 61;
            } else {
                return bitmapInfoList.size();
            }

        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {
            viewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(getActivity().getApplication(), R.layout.item_select, null);
                holder = new viewHolder();
                convertView.setTag(holder);
                // 给holder找view
                holder.imPhoto = (ImageView) convertView.findViewById(R.id.item_select_iv);
                holder.tvSelect = (TextView) convertView.findViewById(R.id.item_select_iv_select);
                holder.imCut = (ImageView) convertView.findViewById(R.id.item_select_iv_cut);
                holder.tvTime = (TextView) convertView.findViewById(R.id.item_select_tv_time);

            } else {
                holder = (viewHolder) convertView.getTag();
            }
            if (null != bitmapInfoList) {
                holder.imPhoto.setImageBitmap(bitmapInfoList.get(i).getmBitmap());
                //String id = TimeFormater.dateLongFormatString(bitmapInfoList.get(i).getTime(), TimeFormater.format2);
                holder.tvTime.setText(TimeFormater.dateLongFormatString(bitmapInfoList.get(i).getTime(), TimeFormater.format2));
            }
            holder.imCut.setVisibility(View.GONE);
            holder.tvSelect.setVisibility(View.GONE);
            if (a == i || b == i) {
                holder.imCut.setVisibility(View.VISIBLE);
            }
            if (a != -1 || b != -1) {
                if (a == -1 && b != -1) {
                    if (b == i) {
                        holder.tvSelect.setVisibility(View.VISIBLE);
                    }
                } else if (a != -1 && b == -1) {
                    if (a == i) {
                        holder.tvSelect.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (a > b) {
                        if (a >= i && b <= i) {
                            holder.tvSelect.setVisibility(View.VISIBLE);
                        }
                    } else if (a < b) {
                        if (b >= i && a <= i) {
                            holder.tvSelect.setVisibility(View.VISIBLE);
                        }
                    }
                }
            }

            return convertView;
        }
    }

    private class viewHolder {
        ImageView imPhoto;
        TextView tvSelect;
        ImageView imCut;
        TextView tvTime;
    }
}
